package com.zte.shopping.action.backend;

import com.zte.shopping.entity.Staff;
import com.zte.shopping.entity.User;
import com.zte.shopping.service.StaffService;
import com.zte.shopping.service.UserService;
import com.zte.shopping.util.Constants;
import com.zte.shopping.util.RandomValidateCode;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * @program: ssm
 * @description: 后端登录控制
 * @author: zxb
 * @create: 2020-06-23 10:37
 **/
@Controller
@RequestMapping("backendLogin")
public class BackendLoginController {

    private  static  final Logger logger= Logger.getLogger(BackendLoginController.class);

    @Autowired
    private UserService userService;
    @Autowired
    private StaffService staffService;
    @Resource
    RandomValidateCode code;

    /*
     * @Author zuti
     * @Description //TODO 初始化去后台的登录页面
     * @Date 22:15 2020/6/23
     * @Param []
     * @return java.lang.String
     **/
    @RequestMapping("/toLogin")
    public  String index(){
        return "backend/login";
    }

    /*
     * @Author zuti
     * @Description //TODO 去管理的主界面
     * @Date 22:15 2020/6/23
     * @Param []
     * @return java.lang.String
     **/
    @RequestMapping("/toMain")
    public String  toMain(){
        return "backend/main";
    }


    /*
     * @Author zuti
     * @Description //TODO 登录页面验证码图片的加载
     * @Date 22:15 2020/6/23
     * @Param [request, response]
     * @return void
     **/
    @RequestMapping("/makeCode")
    public  void makeCode(HttpServletRequest request, HttpServletResponse response) {
        code.getRandcode(request, response);
    }



    /*
     * @Author zuti
     * @Description //TODO ajax验证码校验
     * @Date 22:14 2020/6/23
     * @Param
     * @return
     **/

    @RequestMapping("/ajaxCheckCode")
    public @ResponseBody
    Map<String, Object> ajaxChcekCode(HttpSession session,
                                      @RequestParam(value = "vcode", required = false) String vcode
                                    ) {

        Map<String, Object> codeMap = new HashMap<String, Object>();
        String sessionCode = (String) session.getAttribute(Constants.RANDOM_CODE_KEY);

        if (vcode ==  null || "".equals(vcode)) {
            codeMap.put("reg", "null");
        } else if (vcode.equalsIgnoreCase(sessionCode)) {
            codeMap.put("reg", "true");
        } else {
            codeMap.put("reg", "false");
        }
        return codeMap;
    }


    /*
     * @Author zuti
     * @Description //TODO ajax用户名校验：用于登录用户名校验
     * @Date 22:14 2020/6/23
     * @Param [loginName]
     * @return java.util.Map<java.lang.String,java.lang.Object>
     **/
    @RequestMapping("/ajaxCheckUserName")
    public @ResponseBody
    Map<String, Object> ajaxChcekUserName(@RequestParam("loginName") String loginName) {


        boolean userFlag = this.userService.findUserByLoginName(loginName);
        boolean staffFlag = this.staffService.findStaffByLoginName(loginName);

        logger.debug("BackendLoginController--->ajaxChcekUserName param:loginName="+loginName+" userFlag="+userFlag+" staffFlag="+staffFlag);

        Map<String, Object> resultMap = new HashMap<String, Object>();
        if (userFlag || staffFlag) {
            resultMap.put("reg", "true");
        } else {
            resultMap.put("reg", "false");
        }
        return resultMap;
    }


   /*
   * @Author zuti
   * @Description //TODO 登录进入后台主页面
   * @Date 22:04 2020/6/23
   * @Param  loginName
   * @return
    **/
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public String userLogin(@RequestParam("backendLoginUserName") String loginName,
                            @RequestParam("backendLoginPassword") String password,
                            @RequestParam("state")Integer state,
                            HttpSession session,
                            HttpServletRequest request) {

        Staff staffResult=null;
        Staff staff = new Staff();
//       if(state==1){
//          //userResult = this.userService.userLogin(loginName,password);
//       }else {
           staffResult = this.staffService.staffLogin(loginName,password);
//       }
        if ( staffResult!=null) {

            staff.setLoginName(loginName);
            // 登录成功之后把员工放入Session作用域
            session.setAttribute("staff", staff);
            return "backend/main";
        }else {
            request.setAttribute("mess", "用户名或者密码错误！");
            return "/error";
        }
    }

    /**
     * 退出
     */
    @RequestMapping("/logout")
    public String logout(HttpSession session)
    {
        session.invalidate();
        return "backend/login";
    }





}
